/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.finance;
import shop.core.common.bean.JSONResult;
import shop.core.common.bean.ManageContainer;
import shop.core.common.dao.page.PagerModel;
import shop.core.util.KeyValueHelper;
import shop.core.Services;
import shop.core.util.MD5;
import shop.services.manage.contract.ContractService;
import shop.services.manage.contract.bean.Contract;
import shop.services.manage.finance.FinanceService;
import shop.services.manage.finance.bean.Finance;
import shop.core.BaseController;
import shop.core.RequestHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import shop.services.manage.proxy.ProxyService;
import shop.services.manage.proxy.bean.Proxy;
import shop.services.manage.system.bean.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

/**   
 * @类名称：FinanceAction      
 * @创建人：wzl
 * @创建时间：2018-05-24 下午14:52:27      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/finance/")
public class FinanceAction extends BaseController<Finance> {
    private static final Logger logger = LoggerFactory.getLogger(FinanceAction.class);
    @Autowired
    private FinanceService financeService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private ProxyService proxyService;
    private static final String page_toList = "/manage/finance/financeList";
    private static final String page_toProxyList = "/manage/finance/financeProxyDetail";
    private static final String page_toRecordList = "/manage/finance/financeReceiptList";
    private static final String page_toFinanceCountList = "/manage/finance/financeCountList";
    private static final String page_toContractCountList = "/manage/finance/financeContractList";
    public FinanceService getService() {
        return financeService;
    }

    private FinanceAction(){
        super.page_toList = page_toList;
        super.page_toAdd = null;
        super.page_toEdit = null;
    }

    /**
     * 跳转到委托书列表页面
     *
     * @return
     */
    @RequestMapping("toProxyList")
    public String toProxyList() {
        return page_toProxyList;
    }

    /**
     * 跳转到收款记录列表页面
     *
     * @return
     */
    @RequestMapping("toRecordList")
    public String toRecordList() {
        return page_toRecordList;
    }

    /**
     * 跳转到财务统计列表页面
     *
     * @return
     */
    @RequestMapping("toFinanceCountList")
    public String toFinanceCountList() {
        return page_toFinanceCountList;
    }

    /**
     * 跳转到合同统计列表页面
     *
     * @return
     */
    @RequestMapping("toContractCountList")
    public String toContractCountList() {
        return page_toContractCountList;
    }

    /**
     * 分页查詢收款记录列表
     *
     * @param request 请求
     * @param finance    对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectFinancePage")
    @ResponseBody
    public PagerModel selectFinancePage(HttpServletRequest request, Finance finance) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0)
            offset = start;
        if (length > 0)
            pageSize = length;
        finance.setOffset(offset);
        finance.setPageSize(pageSize);
        //分页查詢收款记录列表
        PagerModel pager = financeService.selectFinancePage(finance);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 登记收款
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertFinance", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertFinance(Finance finance,HttpSession session) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        BigDecimal money=finance.getIncome();
        finance.setCreateUser(user.getId());
        financeService.insert(finance);
        if("proxy".equals(finance.getType())){
            //修改委托书收款金额
            Proxy proxy=new Proxy();
            proxy.setId(finance.getProxyID());
            proxy=proxyService.selectOne(proxy);
            proxy.setProxyGetMoney(proxy.getProxyGetMoney().add(money));
            proxyService.update(proxy);
            //修改委托书所在合同收款金额
            Contract contract=new Contract();
            contract.setId(finance.getContractID());
            contract=contractService.selectOne(contract);
            contract.setContractGetMoney(contract.getContractGetMoney().add(money));
            contractService.update(contract);
        }else{
            //修改合同收款金额
            Contract contract=new Contract();
            contract.setId(finance.getContractID());
            contract=contractService.selectOne(contract);
            contract.setContractGetMoney(contract.getContractGetMoney().add(money));
            contractService.update(contract);
        }
        return jsonResult;
    }
}
